<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

/**
 * 
 * @author Rodrigo Rutkoski Rodrigues <rutkoski@gmail.com>
 *
 */
class SwfHelper extends Helper
{

  /**
   *
   */
  public static $version = "9.0.124";

  /**
   *
   */
  protected static $idc = - 1;

  /**
   *
   */
  protected static $init = false;

  /**
   *
   */
  public function write($movie, $width, $height, $id = null, $flashvars = null, $params = null, $attributes = null)
  {
    $output = '';
    
    if (! self::$init) {
      $output .= $this->javascript->link('swfobject.js');
      self::$init = true;
    }
    
    if (empty($id)) {
      $id = 'flashcontent' . (++ self::$idc);
      $output .= "<div id=\"$id\"></div>";
    }
    
    if (! sy_url_is_absolute($movie)) {
      $movie = sy_fix_url($this->config->get('www_dir') . '/swf/' . $movie);
    }
    $movie = sy_fix_extension($movie, 'swf');
    
    $version = self::$version;
    
    $flashvars = $this->toJsObject((array) $flashvars);
    
    $def_params = array(
      'quality' => "high", 'scale' => "noscale", 
      'wmode' => "transparent", 
      'allowscriptaccess' => "always"
    );
    $params = $this->toJsObject(array_merge($def_params, (array) $params));
    
    $def_attributes = array(
      'id' => $id, 'name' => $id
    );
    $attributes = $this->toJsObject(array_merge($def_attributes, (array) $attributes));
    
    $detect = sy_fix_url($this->config->get('www_dir') . '/swf/expressInstall.swf');
    
    $output .= $this->javascript->block("swfobject.embedSWF('$movie', '$id', '$width', '$height', '$version', '$detect', $flashvars, $params, $attributes);");
    
    return $this->output($output);
  }

  /**
   *
   */
  protected function toJsObject($a)
  {
    if (empty($a)) return 'null';
    
    $o = array();
    foreach ($a as $k => $v) {
      $o[] = "$k: \"$v\"";
    }
    $o = implode(', ', $o);
    
    return "{ $o }";
  }
}

?>